package com.syezon.wifikey.pwd.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.app.WiFiKeyApp;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;
import com.syezon.wifikey.pwd.viewmodel.MyPwdViewModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/11.
 */

public class AddWifiPwdDialog extends Dialog {

    @BindView(R.id.et_ssid)
    EditText mEtSsid;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;

    public AddWifiPwdDialog(@NonNull Context context) {
        this(context, 0);
    }

    public AddWifiPwdDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        setContentView(View.inflate(context, R.layout.dialog_add_wifi_pwd, null));
        ButterKnife.bind(this);
    }

    @Override
    public void show() {
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        super.show();
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                addPwdWifi();
                break;
        }
    }

    private void addPwdWifi() {
        String ssid = mEtSsid.getText().toString();
        String pwd = mEtPwd.getText().toString();
        if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(pwd)) {
            ToastUtils.showShort("WiFi名称获取密码为空！");
        } else {
            if(pwd.length() < 8){
                ToastUtils.showShort(R.string.pwd_short_tip);
                return;
            }
            SaveWifiPwdInfo saveWifiPwdInfo = new SaveWifiPwdInfo();
            saveWifiPwdInfo.setSsid(ssid);
            saveWifiPwdInfo.setPwd(pwd);
            saveWifiPwdInfo.setConnectTime(0);
            saveWifiPwdInfo.setComment("");
            saveWifiPwdInfo.setTime(System.currentTimeMillis());
            WiFiKeyApp.getDatabase().getWifiPwdDao().insertAll(saveWifiPwdInfo);
            MyPwdViewModel.getInstance().getSaveWifiList();
            dismiss();
            ToastUtils.showShort("保存成功！");
        }
    }

}
